package client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StringBufferInputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import bean.TranObject;
import bean.Type;
import bean.User;
import bean.chatcontent;
import server.listen;

public class startclient {

	static final Log logger = LogFactory.getLog(startclient.class);
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			Socket client;
			client = new Socket("192.168.1.101", 6666);
			System.out.println("连接成功");
			final ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
			final ObjectInputStream in = new ObjectInputStream(client.getInputStream());
			TranObject tran = new TranObject();
			tran.setType(Type.LOGIN);
			User user = new User();
			user.setUname("小明");
			user.setUid("6666666");
			tran.setObject(user);
			out.writeObject(tran);
			out.flush();
			try {
				Scanner send = new Scanner(System.in);
				String content = send.nextLine();
				chatcontent chat = new chatcontent();
				TranObject tran2 = new TranObject();
				chat.setContent(content);
				chat.setToid("189eace8084b4903");
				chat.setSendid("6666666");
				tran2.setObject(chat);
				tran2.setType(Type.SENDMESSAGE);
				out.writeObject(tran2);
				out.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			new Thread(new Runnable() {
//
//				public void run() {
//					// TODO Auto-generated method stub
//					while (true) {
//						try {
//							chatcontent chat = (chatcontent)in.readObject();
//							System.out.println(chat.getSendid()+":"+chat.getContent());
//						} catch (IOException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						} catch (ClassNotFoundException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//					}
//				}
//			}).start();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
